﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Lenovo.CFI.BLL;
using Lenovo.CFI.BLL.DicMgr;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;
using TB.Web.UI.WebControls;

namespace Lenovo.CFI.Web.VP
{
    public partial class CfgIdeaApplyList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindData();

                MessageHelper.Prepare(this.Page);
            }
        }

        protected void BtnFilter_Click(object sender, EventArgs e)
        {
            ViewState["user"] = this.TxtUser.Text;
            ViewState["status"] = Convert.ToInt32(this.DdlStatus.SelectedValue);

            this.BindData();
        }

        List<Idea> ideas = null;
        private void BindData()
        {
            string user = null;
            if (ViewState["status"] != null) user = (string)ViewState["user"];
            int? status = null;
            if (ViewState["status"] != null) status = (int)ViewState["status"];
            if (status == -1) status = null;

            List<UserIdeaApp> apps = new UserIdeaAppBl().GetIdeaApp(user, null, null, null, null, status);

            int pageIndex = this.GvList.PageIndex;
            int pageSize = this.GvList.PageSize;
            List<int> ideaIDs = new List<int>();
            for (int i = pageSize * pageIndex; i < pageSize * (pageIndex + 1) && i<apps.Count; i++)
            {
                ideaIDs.Add(apps[i].IdeaID);
            }
            ideas = new IdeaBl().GetIdeaByIds(ideaIDs);
            

            this.GvList.DataSource = apps;
            this.GvList.DataBind();
        }



        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // 设置行号
                ((Literal)(e.Row.FindControl("LtrNo"))).Text = String.Format("{0}", e.Row.DataItemIndex + 1);


                UserIdeaApp app = (UserIdeaApp)e.Row.DataItem;
                Idea idea = this.ideas.Find(j => j.ID == app.IdeaID);

                ((Literal)e.Row.FindControl("LtrName")).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("ideadetail", "id", app.IdeaID.ToString()), idea.Title);
                ((Literal)e.Row.FindControl("LtrApplicant")).Text = app.Applicant;
                ((Literal)e.Row.FindControl("LtrTime")).Text = app.AppTime.ToString("yyyy-MM-dd") + " " + app.AppReason.Format4Html();
                ((Literal)e.Row.FindControl("Status")).Text = app.StatusText;
                ((Literal)e.Row.FindControl("LtrApprover")).Text = app.Approver + " " + app.ApproveReason.Format4Html();

                (((ImageButton)e.Row.FindControl("BtnEdit"))).Visible = app.Status == 1;
            }
        }

        protected void GvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GvList.PageIndex = e.NewPageIndex;
            this.BindData();
        }

        protected void GvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "OpenEdit")
            {
                //UserIdeaApp app = new UserIdeaAppBl().GetIdeaAppByID(Convert.ToInt32(e.CommandArgument));

                ViewState["id"] = Convert.ToInt32(e.CommandArgument);
                this.MpeConfirm.Show();
            }
        }

        

        protected void BtnAgree_Click(object sender, EventArgs e)
        {
            int id = (int)ViewState["id"];

            try
            {
                new UserIdeaAppBl().Agree(id, UserHelper.UserName, this.TxtComment.Text);

                MessageHelper.RegShowJSAjax(this.BtnAgree, "Success!", this.Page);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.RegShowJSAjax(this.BtnAgree, ex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.ExceptionHanderForAjax(ex, this.BtnAgree, this.Page);
            }

            this.BindData();
        }

        protected void BtnReject_Click(object sender, EventArgs e)
        {
            int id = (int)ViewState["id"];

            try
            {
                new UserIdeaAppBl().Reject(id, UserHelper.UserName, this.TxtComment.Text);

                MessageHelper.RegShowJSAjax(this.BtnReject, "Success!", this.Page);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.RegShowJSAjax(this.BtnReject, ex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.ExceptionHanderForAjax(ex, this.BtnReject, this.Page);
            }

            this.BindData();
        }


    }
}